package com.hducms.common.util;

import java.io.IOException;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import com.hducms.cms.entity.front.ChannelResponse;
import com.hducms.cms.entity.front.ContentM;
import com.hducms.cms.entity.front.ContentResponse;
import com.hducms.common.web.ResponseUtils;

public class ClientUtils {
	public static void resultResponse(HttpServletResponse response,Object object,String publickey) {
		ObjectMapper objectMapper = new ObjectMapper();
		String msg = null;
		try {
			msg = objectMapper.writeValueAsString(object);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if (publickey!=null) {
			try {
				String m = RSAUtils.encryptToStringByPublicKey(msg.getBytes("UTF-8"), publickey);
				System.err.println(m);
				ResponseUtils.renderJson(response, m);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else {
			ResponseUtils.renderJson(response,msg);
			return;
		}
	}
	
	public static String requestTransform(HttpServletRequest request) {
		if (request.getQueryString()!=null) {
			String temp = request.getQueryString().substring(10);
			String result = temp.replace("%20", " ");
			return result;
		}
		else {
			return null;
		}
	}
	public static void main(String[] args) {
//		ContentM contentM = new ContentM(1, "t", "txt", "data", "author");
//		ContentResponse response= new ContentResponse(contentM);
//		ObjectMapper objectMapper = new ObjectMapper();
		Scanner scanner = new Scanner(System.in);
		String result = scanner.nextLine();
		try {
//			String result = objectMapper.writeValueAsString(response);
			System.out.println(result);
			ObjectMapper objectMapper2 = new ObjectMapper();
			ContentResponse get2 =objectMapper2.readValue(
					result,
					new TypeReference<ContentResponse>() {}
					); 
			System.out.println(get2);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
